package daos;

import hibernate.HibernateUtil;
import java.util.List;
import negocio.Agenda;
import negocio.Medico;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class AgendaDAO {
	
private static AgendaDAO instancia = null;
	
	private static SessionFactory sf = null;

	public static AgendaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new AgendaDAO();
		} 
		return instancia;
	}
	
	public void guardarAgenda(Agenda a){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(a);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void modificarAgenda(Agenda a){
		Session session = sf.openSession();
		session.beginTransaction();
		session.update(a);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void eliminarAgenda(Agenda ag){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(ag);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Agenda> obtenerAgendas(){
		Session session = sf.openSession();
		List<Agenda> agendas = session.createQuery("from Agenda").list();
		session.close();
		return agendas;
	}
	
	public Agenda obtenerAgenda(Medico medico){
		Session session = sf.openSession();
		Query query = session.createQuery(
				"from Agenda a where a.medico =:medico").setParameter("medico", medico);
		Agenda agenda =  (Agenda)query.uniqueResult();
		session.close();
		return agenda;
	}
}
